﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using ScFramework.Core;
using UnityEngine.UI;


public partial class WorkshopLogic : LogicModuleBase<InputManager, SpriteManager>, ICombineSubUI
{
    public class FlipRotateManager
    {
        public void FlipTrigged(int id)
        {
            MeshEntityData EntityToFlip;
            EntityToFlip = currentEntity.Find(e => e.ID == id);

            if (EntityToFlip.Body.PresentPos == 1) //1. in workshop
            {
                var preX = EntityToFlip.Body.MeshPos.x;
                var preY = EntityToFlip.Body.MeshPos.y;

                List<Vector3Int> subList = ListTransform(EntityToFlip);
                foreach (var i in subList)
                {
                    Grid.GridList[preX + i.x][preY + i.y].isOccupied = false;
                    Grid.GridList[preX + i.x][preY + i.y].occupyID = default;
                }
            }

            EntityToFlip.Body.IsFliped = !EntityToFlip.Body.IsFliped;

            if (EntityToFlip.Body.PresentPos == 1) //1. in workshop
            {
                if (IsFRCollideJudge(EntityToFlip))
                {
                    EntityToFlip.Body.IsFliped = !EntityToFlip.Body.IsFliped;
                    var preX = EntityToFlip.Body.MeshPos.x;
                    var preY = EntityToFlip.Body.MeshPos.y;

                    List<Vector3Int> subList = ListTransform(EntityToFlip);
                    foreach (var i in subList)
                    {
                        Grid.GridList[preX + i.x][preY + i.y].isOccupied = true;
                        Grid.GridList[preX + i.x][preY + i.y].occupyID = EntityToFlip.ID;
                    }
                    Debug.LogWarning("Flip Conflict");
                    return;
                   
                }
                else
                {
                    List<Vector3Int> subList = ListTransform(EntityToFlip);
                    var preX = EntityToFlip.Body.MeshPos.x;
                    var preY = EntityToFlip.Body.MeshPos.y;
                    foreach (var i in subList)
                    {
                        Grid.GridList[preX + i.x][preY + i.y].isOccupied = true;
                        Grid.GridList[preX + i.x][preY + i.y].occupyID = EntityToFlip.ID;
                    }
                }
                Grid.RefreshWorkshopGrid();
            }
        }

        public void RotateTrigged(int id)
        {
            MeshEntityData EntityToRotate;
            EntityToRotate = currentEntity.Find(e => e.ID == id);



            if (EntityToRotate.Body.PresentPos == 1) //1. in workshop
            {
                var preX = EntityToRotate.Body.MeshPos.x;
                var preY = EntityToRotate.Body.MeshPos.y;

                List<Vector3Int> subList = ListTransform(EntityToRotate);
                foreach (var i in subList)
                {
                    Grid.GridList[preX + i.x][preY + i.y].isOccupied = false;
                    Grid.GridList[preX + i.x][preY + i.y].occupyID = default;
                }
            }

            EntityToRotate.Body.MeshRotate = (EntityToRotate.Body.MeshRotate + 1) % 4;

            if (EntityToRotate.Body.PresentPos == 1) //1. in workshop
            {
                if (IsFRCollideJudge(EntityToRotate))
                {
                    //DraggedEntityShow.DeleteEntitySprite(id, EntityToRotate.Body.MeshPos.x, EntityToRotate.Body.MeshPos.y, "InGrid");
                    EntityToRotate.Body.MeshRotate = (EntityToRotate.Body.MeshRotate + 3) % 4;
                    var preX = EntityToRotate.Body.MeshPos.x;
                    var preY = EntityToRotate.Body.MeshPos.y;

                    List<Vector3Int> subList = ListTransform(EntityToRotate);
                    foreach (var i in subList)
                    {
                        Grid.GridList[preX + i.x][preY + i.y].isOccupied = true;
                        Grid.GridList[preX + i.x][preY + i.y].occupyID = EntityToRotate.ID;
                    }
                    Debug.LogWarning("Rotate Conflict");
                    return;
                    //DragManager.BackToBackpack(id);
                }
                else
                {
                    List<Vector3Int> subList = ListTransform(EntityToRotate);
                    var preX = EntityToRotate.Body.MeshPos.x;
                    var preY = EntityToRotate.Body.MeshPos.y;
                    foreach (var i in subList)
                    {
                        Grid.GridList[preX + i.x][preY + i.y].isOccupied = true;
                        Grid.GridList[preX + i.x][preY + i.y].occupyID = EntityToRotate.ID;
                    }
                }
                Grid.RefreshWorkshopGrid();
            }
        }

        private bool IsFRCollideJudge(MeshEntityData toShow)
        {
            if(DragManager.IsDeltaXYOverFlow(toShow.Body.MeshPos.x,toShow.Body.MeshPos.y,toShow)||DragManager.IsOccupied(toShow.Body.MeshPos.x, toShow.Body.MeshPos.y, toShow))
            {
                Debug.Log("变换后冲突");
                return true;
            }
            return false;
        }

        public static Vector3Int VectorTransform(MeshEntityData toTrans, Vector3Int ori)
        {
            Vector3Int ansV = new Vector3Int();
            ansV = ori;

            if (toTrans.Body.IsFliped)
            {
                ansV.x *= -1;
            }
            switch (toTrans.Body.MeshRotate)
            {
                case 0:
                    break;
                case 1:
                    {
                        int forSwap = ansV.x;
                        ansV.x = -1 * ansV.y;
                        ansV.y = forSwap;
                        break;
                    }
                case 2:
                    {
                        ansV.x = -1 * ansV.x;
                        ansV.y = -1 * ansV.y;
                        break;
                    }
                case 3:
                    {
                        int forSwap = ansV.x;
                        ansV.x = ansV.y;
                        ansV.y = -1 * forSwap;
                        break;
                    }
                default:
                    break;
            }
            return ansV;
        }

        public static List<Vector3Int> ListTransform(MeshEntityData toTrans)
        {
            List<Vector3Int> generalList = new List<Vector3Int>();
            foreach(var x in toTrans.Colliders)
            {
                generalList.AddRange(x.LocalMeshPos);
            }
            if (toTrans.Body.IsFliped)
            {
                List<Vector3Int> tmpList = new List<Vector3Int>();
                foreach(var v in generalList)
                {
                    Vector3Int tmp = v;
                    tmp.x *= -1;
                    tmpList.Add(tmp);
                }
                generalList = tmpList;
            }

            switch (toTrans.Body.MeshRotate)
            {
                case 0:
                    break;
                case 1:
                    {
                        List<Vector3Int> tmpList = new List<Vector3Int>();
                        foreach (var v in generalList)
                        {
                            Vector3Int tmp = v;
                            int forSwap = tmp.x;
                            tmp.x = -1 * tmp.y;
                            tmp.y = forSwap;
                            tmpList.Add(tmp);
                        }
                        generalList = tmpList;
                        break;
                    }
                case 2:
                    {
                        List<Vector3Int> tmpList = new List<Vector3Int>();
                        foreach (var v in generalList)
                        {
                            Vector3Int tmp = v;
                            tmp.x = -1 * tmp.x;
                            tmp.y = -1 * tmp.y;
                            tmpList.Add(tmp);
                        }
                        generalList = tmpList;
                        break;
                    }
                case 3:
                    {
                        List<Vector3Int> tmpList = new List<Vector3Int>();
                        foreach (var v in generalList)
                        {
                            Vector3Int tmp = v;
                            int forSwap = tmp.x;
                            tmp.x = tmp.y;
                            tmp.y = -1 * forSwap;
                            tmpList.Add(tmp);
                        }
                        generalList = tmpList;
                        break;
                    }
                default:
                    break;
            }
            return generalList;
        }
       
        public static GameObject SpriteTransform(MeshEntityData toTrans, GameObject obj ,bool zoom)    //直接修改 不安全
        {
            //GameObject obj = new GameObject();
            //obj = objo;
            Vector3 newScale = new Vector3(1, 1, 1);

            if(zoom == true)
            {
                List<Vector3Int> subList = new List<Vector3Int>();
                foreach (var e in toTrans.Colliders)
                {
                    subList.AddRange(e.LocalMeshPos);
                }

                int xmax = subList[0].x;
                int ymax = subList[0].y;
                int xmin = subList[0].x;
                int ymin = subList[0].y;
                foreach (var xx in subList)
                {
                    if (xx.x > xmax)
                        xmax = xx.x;
                    if (xx.x < xmin)
                        xmin = xx.x;
                    if (xx.y > ymax)
                        ymax = xx.y;
                    if (xx.y < ymin)
                        ymin = xx.y;
                }
                newScale.x *= xmax - xmin + 1;
                newScale.y *= ymax - ymin + 1;
            }

            if (toTrans.Body.IsFliped)
            {
                newScale.x *= -1;
            }

            obj.transform.localScale = newScale;

            switch (toTrans.Body.MeshRotate)
            {
                case 0:
                    {
                        obj.transform.localRotation = new Quaternion { eulerAngles = new Vector3(0, 0, 0) };
                        break;
                    }
                case 1:
                    {
                        obj.transform.localRotation = new Quaternion { eulerAngles = new Vector3(0, 0, 90) };
                        break;
                    }
                case 2:
                    {
                        obj.transform.localRotation = new Quaternion { eulerAngles = new Vector3(0, 0, 180) };
                        break;
                    }
                case 3:
                    {
                        obj.transform.localRotation = new Quaternion { eulerAngles = new Vector3(0, 0, 270) };
                        break;
                    }
                default:
                    break;
            }
            return obj;
        }
    }
}


